package dp.ua.epic.pages;

import dp.ua.epic.urlcoding.MountNonVersionedHybrid;
import epic.domain.Hero;
import epic.services.HeroService;
import java.io.Serializable;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.validation.validator.PatternValidator;
import org.wicketstuff.annotation.mount.MountPath;

/**
 * Class that responsible for start page and also are page by default
 * 
 * @author Artem Kozlov
 */
@MountPath(path = "start")
@MountNonVersionedHybrid
public class Start extends BasePage {

    private static final long serialVersionUID = 1L;
    
    @SpringBean
    private HeroService heroService;

    public Start(final PageParameters parameters) {
        final Form form = new Form("heroForm") {

            @Override
            public void onSubmit() {
                setResponsePage(Choose.class);
            }
        };
        
        final TextField edit = new TextField("heroNameEdit", new Model() {

            @Override
            public void setObject(Serializable object) {
                Hero newHero = heroService.createNewHero((String) object);
                getEpicSession().setHero(newHero);
            }
        });
        
        final Button button = new Button("proceedButton") {

            @Override
            public void onSubmit() {
                setResponsePage(Choose.class);
            }
        };

        form.add(edit.setRequired(true).add(new PatternValidator("([A-Za-z])*")));
        form.add(new FeedbackPanel("heroNameErrorLabel"));
        form.add(button);
        add(form);
    }
}